/**
 * 
 */
package com.antilia.infomaresme.pages.edit;

import java.util.Date;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.odlabs.wiquery.ui.datepicker.DatePicker;

import com.antilia.infomaresme.EnumDropDownChoice;
import com.antilia.infomaresme.data.dao.interfaces.ICategoriesDao;
import com.antilia.infomaresme.data.dao.interfaces.IItemsDao;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;
import com.antilia.infomaresme.link.JQIcon;
import com.antilia.infomaresme.link.JqAjaxLink;
import com.antilia.infomaresme.resources.InfoModalWindow;
import com.google.inject.Inject;

/**
 * @author Ernesto
 *
 */
public class PropertiesPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@Inject
	private IItemsDao iItemsDao;
	
	private ModalWindow modal;
	
	private Label categoryTitle;
	
	@Inject
	private ICategoriesDao categoriesDao;
	
	private Label geographicLocation;
	
	/**
	 * @param id
	 */
	public PropertiesPanel(String id, final IModel<Item> model) {
		super(id, model);
		
		modal = new InfoModalWindow("modal");
		add(modal);
		
		add(new TextField<String>("title").setRequired(true));
		add(new TextField<String>("shortDescription")); 
		add(new TextField<Double>("price").add(new IValidator<Double>() {
			
			private static final long serialVersionUID = 1L;

			public void validate(IValidatable<Double> validatable) {
				if(validatable.getValue() != null && validatable.getValue()<0) {
					validatable.error(new ValidationError().setMessage("Price cannote be negative"));
				}
			}
		})); 
		add(new DatePicker<Date>("otherDate"));
		
		add(new CheckBox("allowMessages"));
		
		add(new EnumDropDownChoice<VisibilityStatus>("status", VisibilityStatus.class));
		
		add(categoryTitle = new Label("categoryTitle")); 
		categoryTitle.setOutputMarkupId(true);
		
		JqAjaxLink chooseCategory = new JqAjaxLink("chooseCategory", JQIcon.ui_icon_zoomin, new ResourceModel("selecteACategory")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				modal.setTitle(new ResourceModel("selecteACategory"));
				modal.setContent(new AsignCategoryPanel(modal.getContentId(), categoriesDao.findTopLevel(), 
						new ResourceModel("categoryPath"), new ResourceModel("selecteACategoryLong")) {
					
					private static final long serialVersionUID = 1L;

					@Override
					protected void onCategorySelected(AjaxRequestTarget target, Category category) {
						modal.close(target);
						model.getObject().setCategoryPath(category);
						target.add(categoryTitle);
					}
				});
				modal.show(target);
			}
		};
		add(chooseCategory);
		
		add(geographicLocation = new Label("geographicLocation")); 
		geographicLocation.setOutputMarkupId(true);
		
		JqAjaxLink chooseGeographicLocation = new JqAjaxLink("chooseGeographicLocation", JQIcon.ui_icon_zoomin, new ResourceModel("selecteALocation")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				modal.setTitle(new ResourceModel("selecteALocation"));
				modal.setContent(new AsignCategoryPanel(modal.getContentId(), categoriesDao.findTopLevelGeographic(), 
						new ResourceModel("locations"), new ResourceModel("selecteALocationLong")) {
					
					private static final long serialVersionUID = 1L;

					@Override
					protected void onCategorySelected(AjaxRequestTarget target, Category category) {
						modal.close(target);
						model.getObject().setGeographicPath(category);
						target.add(geographicLocation);
					}
				});
				modal.show(target);
			}
		};
		
		add(chooseGeographicLocation);
		
	}

	public IItemsDao getiItemsDao() {
		return iItemsDao;
	}

	public void setiItemsDao(IItemsDao iItemsDao) {
		this.iItemsDao = iItemsDao;
	}

	public ICategoriesDao getCategoriesDao() {
		return categoriesDao;
	}

	public void setCategoriesDao(ICategoriesDao categoriesDao) {
		this.categoriesDao = categoriesDao;
	}

}
